#!/usr/bin/env python
# -*- coding:utf-8 -*- 

from flask_restful import Resource, reqparse
from flask import jsonify

from ...controller.userController import StudentController
from ...utils import commons
from ...utils.response_code import RET
from ...utils.validate_operate import verify_operator_object, verify_user_role


class StudentResource(Resource):

    # get
    @classmethod
    def get(cls, userID=None):
        if userID:
            kwargs = {
                'userID': userID
            }

            res = StudentController.get(**kwargs)
            if res['code'] == RET.OK:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])
            else:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])

        parser = reqparse.RequestParser()
        parser.add_argument('classID', location='args', required=False, help='classID参数类型不正确或缺失')
        parser.add_argument('name', location='args', required=False, help='name参数类型不正确或缺失')
        parser.add_argument('age', location='args', required=False, help='age参数类型不正确或缺失')
        parser.add_argument('agenda', location='args', required=False, help='agenda参数类型不正确或缺失')
        parser.add_argument('addTime', location='args', required=False, help='addTime参数类型不正确或缺失')

        parser.add_argument('Page', location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', location='args', required=False, help='Size参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = StudentController.get(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalPage=res['totalPage'],
                           totalCount=res['totalCount'])
        else:
            return jsonify(code=res['code'], message=res['message'], data=res['data'])

            # delete

    @classmethod
    # @verify_user_role([2, 3])
    def delete(cls):
        # if userID:
        #     kwargs = {
        #         'userID': userID
        #     }
        #
        # else:
        parser = reqparse.RequestParser()
        parser.add_argument('name', location='json', required=False, help='name参数类型不正确或缺失')
        parser.add_argument('age', location='json', required=False, help='age参数类型不正确或缺失')
        parser.add_argument('sex', location='json', required=False, help='sex')
        parser.add_argument('addTime', location='json', required=False, help='addTime参数类型不正确或缺失')

            # Pass in the ID list for multiple deletions
        parser.add_argument('userID', type=str, location='json', required=False, help='userID')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = StudentController.delete(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # put
    @classmethod
    # @verify_operator_object([1], StudentController)
    def put(cls):

        parser = reqparse.RequestParser()
        parser.add_argument('userID', location='json', required=False, help='classID参数类型不正确或缺失')
        parser.add_argument('name', location='json', required=False, help='name参数类型不正确或缺失')
        parser.add_argument('age', location='json', required=False, help='age参数类型不正确或缺失')
        parser.add_argument('sex', location='json', required=False, help='sex')
        parser.add_argument('addTime', location='json', required=False, help='addTime参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = StudentController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # add
    @classmethod
    # @verify_user_role([2, 3])
    def post(cls):
        '''
        StudentList: Pass in values in JSON format to batch add
        eg.[{k1:v1,k2:v2,...},...]
        '''
        parser = reqparse.RequestParser()
        parser.add_argument('StudentList', type=str, location='form', required=False, help='StudentList参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        if kwargs.get('StudentList'):
            res = StudentController.add_list(**kwargs)

        else:
            parser.add_argument('userID', location='json', required=False, help='userID')
            parser.add_argument('name', location='json', required=False, help='name')
            parser.add_argument('isdelete', location='json', required=False, help='isdelete')
            parser.add_argument('age', location='json', required=False, help='age参数类型不正确或缺失')
            parser.add_argument('sex', location='json', required=False, help='sex')
            parser.add_argument('addTime', location='json', required=False, help='addTime参数类型不正确或缺失')

            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)

            res = StudentController.add(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])
